package io.gitee.kotle.android.utils

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.core.view.*
import androidx.fragment.app.Fragment

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/24 9:46 上午
 *
 */

/**
 * 状态栏文字颜色，只有白色和黑色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Activity.isStatusBarBlackTextColor(isBlackStatusBarTextColor: Boolean) {
    window.isStatusBarBlackTextColor(isBlackStatusBarTextColor)
}


/**
 * 导航栏按钮颜色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Activity.isNavigationBarBlackTextColor(isBlackNavigationBarTextColor: Boolean) {
    window.isNavigationBarBlackTextColor(isBlackNavigationBarTextColor)
}

/**
 * 设置导航栏颜色
 */
fun Activity.isSystemBarBlackTextColor(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
) {
    window.isSystemBarBlackTextColor(isBlackStatusBarTextColor, isBlackNavigationBarTextColor)
}


/**
 * 状态栏文字颜色，只有白色和黑色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Fragment.isStatusBarBlackTextColor(isBlackStatusBarTextColor: Boolean) {
    appCompatActivity.isStatusBarBlackTextColor(isBlackStatusBarTextColor)
}

/**
 * 导航栏按钮颜色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Fragment.isNavigationBarBlackTextColor(isBlackNavigationBarTextColor: Boolean) {
    appCompatActivity.isNavigationBarBlackTextColor(isBlackNavigationBarTextColor)
}

/**
 * 设置导航栏颜色
 */
fun Fragment.isSystemBarBlackTextColor(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
) {
    appCompatActivity.isSystemBarBlackTextColor(
        isBlackStatusBarTextColor,
        isBlackNavigationBarTextColor
    )
}

/**
 * 状态栏文字颜色，只有白色和黑色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Window.isStatusBarBlackTextColor(isBlackStatusBarTextColor: Boolean) {
    WindowCompat.getInsetsController(this, decorView).apply {
        isAppearanceLightStatusBars = isBlackStatusBarTextColor
    }
}

/**
 * 导航栏的按钮颜色
 */
@Deprecated("请使用支持导航栏和状态栏的函数", ReplaceWith("isSystemBarBlackTextColor"))
fun Window.isNavigationBarBlackTextColor(isBlackNavigationBarTextColor: Boolean) {
    WindowCompat.getInsetsController(this, decorView).apply {
        isAppearanceLightNavigationBars = isBlackNavigationBarTextColor
    }
}

/**
 * 系统导航栏的颜色
 */
fun Window.isSystemBarBlackTextColor(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
) {
    //处理状态栏文字颜色
    if (isBlackStatusBarTextColor != null || isBlackNavigationBarTextColor != null) {
        WindowCompat.getInsetsController(this, decorView).apply {
            if (isBlackStatusBarTextColor != null) {
                isAppearanceLightStatusBars = isBlackStatusBarTextColor
            }
            if (isBlackNavigationBarTextColor != null) {
                isAppearanceLightNavigationBars = isBlackNavigationBarTextColor
            }
        }
    }
}

/**
 * 透明状态栏
 */
inline fun Activity.transparentStatusBar(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
    crossinline insetsBlock: (
        insets: WindowInsetsCompat,
        statusBarsHeight: Int, navigationBarHeight: Int
    ) -> WindowInsetsCompat = { insets, _, _ -> insets }
) {
    window.transparentStatusBar(
        isBlackStatusBarTextColor,
        isBlackNavigationBarTextColor,
        insetsBlock
    )
}

/**
 * 透明状态栏
 */
inline fun Fragment.transparentStatusBar(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
    crossinline insetsBlock: (
        insets: WindowInsetsCompat,
        statusBarsHeight: Int, navigationBarHeight: Int
    ) -> WindowInsetsCompat = { insets, _, _ -> insets }
) {
    appCompatActivity.transparentStatusBar(
        isBlackStatusBarTextColor,
        isBlackNavigationBarTextColor,
        insetsBlock
    )
}

/**
 * 透明状态栏
 */
inline fun Window.transparentStatusBar(
    isBlackStatusBarTextColor: Boolean? = null,
    isBlackNavigationBarTextColor: Boolean? = null,
    crossinline insetsBlock: (
        insets: WindowInsetsCompat,
        statusBarsHeight: Int, navigationBarHeight: Int
    ) -> WindowInsetsCompat = { insets, _, _ -> insets }
) {
    //预留导航栏的空间
    ViewCompat.setOnApplyWindowInsetsListener(decorView) { _, insets ->
        insetsBlock.invoke(
            insets,
            insets.getInsets(WindowInsetsCompat.Type.statusBars()).top,
            insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
        )
    }
    //设置系统不要给状态栏和导航栏预留空间，否则无法透明状态栏
    WindowCompat.setDecorFitsSystemWindows(this, false)
    //设置状态栏颜色透明
    statusBarColor = Color.TRANSPARENT
    //处理状态栏文字颜色
    if (isBlackStatusBarTextColor != null || isBlackNavigationBarTextColor != null) {
        WindowCompat.getInsetsController(this, decorView).apply {
            if (isBlackStatusBarTextColor != null) {
                isAppearanceLightStatusBars = isBlackStatusBarTextColor
            }
            if (isBlackNavigationBarTextColor != null) {
                isAppearanceLightNavigationBars = isBlackNavigationBarTextColor
            }
        }
    }
}

/**
 * 全屏activity监听
 */
fun Window?.fullScreen(
    isFullScreen: Boolean,
    enableCutoutEdges: Boolean = true,/*允许绘制到耳朵区域*/
    statusBar: Boolean = true,/*隐藏状态栏*/
    navigationBar: Boolean = true,/*隐藏导航栏*/
    fitsSystemWindows: Boolean = !isFullScreen
) {
    val window = this ?: return
    val insetsController = WindowCompat.getInsetsController(this, decorView) ?: return
    val lp = window.attributes
    if (isFullScreen) {
        // 延伸显示区域到耳朵区
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = if (enableCutoutEdges) {
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            } else {
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
            }
            window.attributes = lp
        }
        insetsController.apply {
            systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            if (statusBar) {
                hide(WindowInsetsCompat.Type.statusBars())
            }
            if (navigationBar) {
                hide(WindowInsetsCompat.Type.navigationBars())
            }
        }
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
            window.attributes = lp
        }
        insetsController.apply {
            if (statusBar) {
                show(WindowInsetsCompat.Type.statusBars())
            }
            if (navigationBar) {
                show(WindowInsetsCompat.Type.navigationBars())
            }
        }
    }
    WindowCompat.setDecorFitsSystemWindows(this@fullScreen, fitsSystemWindows)
}

/**
 * 全屏
 */
fun View?.fullScreen(
    isFullScreen: Boolean,
    enableCutoutEdges: Boolean = true,/*允许绘制到耳朵区域*/
    statusBar: Boolean = true,/*隐藏状态栏*/
    navigationBar: Boolean = true,/*隐藏导航栏*/
    fitsSystemWindows: Boolean = !isFullScreen
) {
    this?.appCompatActivity.fullScreen(
        isFullScreen,
        enableCutoutEdges,
        statusBar,
        navigationBar,
        fitsSystemWindows
    )
}

/**
 * 全屏
 */
fun Activity?.fullScreen(
    isFullScreen: Boolean,
    enableCutoutEdges: Boolean = true,/*允许绘制到耳朵区域*/
    statusBar: Boolean = true,/*隐藏状态栏*/
    navigationBar: Boolean = true,/*隐藏导航栏*/
    fitsSystemWindows: Boolean = !isFullScreen
) {
    this?.window?.fullScreen(
        isFullScreen,
        enableCutoutEdges,
        statusBar,
        navigationBar,
        fitsSystemWindows
    )
}